<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
  <head>
    <meta charset="utf-8" />
    <meta name="generator" content="pandoc" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, user-scalable=yes"
    />
    <title>30-python-snippets</title>
    <style type="text/css">
      code {
        white-space: pre-wrap;
      }
      span.smallcaps {
        font-variant: small-caps;
      }
      span.underline {
        text-decoration: underline;
      }
      div.column {
        display: inline-block;
        vertical-align: top;
        width: 50%;
      }
    </style>
  </head>
  <body>
    <h1 id="helpful-python-snippets-that-you-can-learn-in-30-seconds-or-less">
      30 Helpful Python Snippets That You Can Learn in 30 Seconds or Less
    </h1>
    <blockquote>
      <p>
        Short Python snippets that you can quickly learn and use in your work or
        personal needs
      </p>
    </blockquote>
    <p>
      <a
        href="https://fatosmorina.medium.com/?source=post_page-----69bb49204172--------------------------------"
        ><img
          src="https://miro.medium.com/fit/c/56/56/1*K5svz0DwQPKGfnX0LJZozw.jpeg"
          alt="Fatos Morina"
      /></a>
    </p>
    <figure>
      <img
        src="https://miro.medium.com/max/60/0*a4SfQa9ogzbuspzy?q=20"
        alt="Image for post"
      />
      <figcaption>Image for post</figcaption>
    </figure>
    <figure>
      <img
        src="https://miro.medium.com/max/9698/0*a4SfQa9ogzbuspzy"
        alt="Image for post"
      />
      <figcaption>Image for post</figcaption>
    </figure>
    <p>
      Photo by
      <a
        href="https://unsplash.com/@jantined?utm_source=medium&amp;utm_medium=referral"
        >Jantine Doornbos</a
      >
      on
      <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral"
        >Unsplash</a
      >
    </p>
    <p>
      Python represents one of the most popular languages that many people use
      it in data science and machine learning, web development, scripting,
      automation, etc.
    </p>
    <p>
      Part of the reason for this popularity is its simplicity and easiness to
      learn it.
    </p>
    <p>
      If you are reading this, then it is highly likely that you already use
      Python or at least have an interest in it.
    </p>
    <p>
      In this article, we will briefly see 30 short code snippets that you can
      understand and learn in 30 seconds or less.
    </p>
    <p>
      The following method checks whether the given list has duplicate elements.
      It uses the property of <em>set()</em> which removes duplicate elements
      from the list.
    </p>
    <p>
      This method can be used to check if two strings are anagrams. An anagram
      is a word or phrase formed by rearranging the letters of a different word
      or phrase, typically using all the original letters exactly once.
    </p>
    <p>This snippet can be used to check the memory usage of an object.</p>
    <p>This method returns the length of a string in bytes.</p>
    <p>
      This snippet can be used to print a string <em>n</em> times without having
      to use loops to do it.
    </p>
    <p>
      This snippet simply uses the method <em>title()</em> to capitalize first
      letters of every word in a string.
    </p>
    <p>This method chunks a list into smaller lists of a specified size.</p>
    <p>
      This method removes falsy values (False, None, 0 and “") from a list by
      using <em>filter()</em>.
    </p>
    <p>This snippet can be used to transpose a 2D array.</p>
    <p>
      You can do multiple comparisons with all kinds of operators in a single
      line.
    </p>
    <p>
      This snippet can be used to turn a list of strings into a single string
      with each element from the list separated by commas.
    </p>
    <p>This method gets vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) found in a string.</p>
    <p>
      This method can be used to turn the first letter of the given string into
      lowercase.
    </p>
    <p>
      The following methods flatten a potentially deep list using recursion.
    </p>
    <p>
      This method finds the difference between two iterables by keeping only the
      values that are in the first one.
    </p>
    <p>
      The following method returns the difference between two lists after
      applying a given function to each element of both lists.
    </p>
    <p>You can call multiple functions inside a single line.</p>
    <p>
      The following method checks whether a list has duplicate values by using
      the fact that <em>set()</em> contains only unique elements.
    </p>
    <p>The following method can be used to merge two dictionaries.</p>
    <p>In Python 3.5 and above, you can also do it like the following:</p>
    <p>
      The following method can be used to convert two lists into a dictionary.
    </p>
    <p>
      This snippet shows that you can use <em>enumerate</em> to get both the
      values and the indexes of lists.
    </p>
    <p>
      This snippet can be used to calculate the time it takes to execute a
      particular code.
    </p>
    <p>
      You can have an <em>else</em> clause as part of a
      <em>try/except</em> block, which is executed if no exception is thrown.
    </p>
    <p>This method returns the most frequent element that appears in a list.</p>
    <p>This method checks whether a given string is a palindrome.</p>
    <p>
      The following snippet shows how you can write a simple calculator without
      the need to use if-else conditions.
    </p>
    <p>
      This snippet can be used to randomize the order of the elements in a list.
      Note that shuffle works in place, and returns <em>None</em>.
    </p>
    <p>
      This method flattens a list similarly like [].concat(…arr) in JavaScript.
    </p>
    <p>
      This is a really quick way for swapping two variables without having to
      use an additional one.
    </p>
    <p>
      This snippet shows how you can get a default value in case a key you are
      looking for is not included in the dictionary.
    </p>
    <p>
      <a
        href="https://towardsdatascience.com/30-helpful-python-snippets-that-you-can-learn-in-30-seconds-or-less-69bb49204172"
        >Source</a
      >
    </p>
  </body>
</html>
